package views;

import com.grupp42.te.R;

import core.Main;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * Settings menu gui class
 * 
 * @author Christian
 * 
 */
public class Settings extends Activity {

	// The number of spinners, needed for block the listeners at creation
	private int startUp = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		/* Sets the view to the settings layout */
		setContentView(R.layout.settings);

		/* Creates the different spinners */

		Spinner weekSpinner = (Spinner) findViewById(R.id.WeekSpinner);
		Spinner updateSpinner = (Spinner) findViewById(R.id.UpdateSpinner);
		Spinner colorSpinner = (Spinner) findViewById(R.id.ColorSpinner);

		/* Adds the spinners to the view */
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.setttings_week_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		weekSpinner.setAdapter(adapter);

		adapter = ArrayAdapter.createFromResource(this,
				R.array.setttings_update_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		updateSpinner.setAdapter(adapter);

		adapter = ArrayAdapter.createFromResource(this,
				R.array.setttings_color_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		colorSpinner.setAdapter(adapter);

		/* Listeners for the spinners */
		weekSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// 0 = week, 1 = day
				if (!startUpBlock()) {
					Main.getAppSettings().setScheduleView(position);
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {
				System.out.println("ubselected");
			}
		});
		updateSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// 0 = daily, 1 = 3days, 2= weekly , 3= never
				if (!startUpBlock())
					Main.getAppSettings().setUpdateIntervall(position);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				System.out.println("ubselected");
			}
		});
		colorSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// 0 = mono, 1 = colors
				if (!startUpBlock())
					Main.getAppSettings().setColorScheme(position);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				System.out.println("ubselected");
			}
		});
		// Load the settings to spinners
		weekSpinner.setSelection(Main.getAppSettings().getScheduleView(),
				true);
		updateSpinner.setSelection(
				Main.getAppSettings().getUpdateIntervall(), true);
		colorSpinner.setSelection(Main.getAppSettings().getColorScheme(),
				true);

	}

	/* Blocks the listener to send at creation */
	private boolean startUpBlock() {
		this.startUp--;
		if (startUp < 0)
			return false;
		else
			return true;

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
}
